﻿using System;
using System.ComponentModel;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;
using SC2TV.ViewModel;

namespace SC2TV
{
    public partial class MainPage
    {
        public MainPage()
        {
            Loaded += MainPage_Loaded;
        }

        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            base.OnBackKeyPress(e);
            Application.Current.Terminate();
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            string password;
            IsolatedStorageSettings.ApplicationSettings.TryGetValue("bb_password", out password);
            if (!String.IsNullOrEmpty(password))
                NavigationService.Navigate(new Uri("/ClientPage.xaml", UriKind.Relative));
            else
                InitializeComponent();
        }

        private async void SignInClick(object sender, RoutedEventArgs e)
        {
            var vm = DataContext as MainViewModel;

            var statusCode = await vm.Load();
            if (statusCode == HttpStatusCode.OK)
                NavigationService.Navigate(new Uri("/ClientPage.xaml", UriKind.Relative));
            else if (statusCode != 0)
                MessageBox.Show(statusCode.ToString());
        }

    }
}